import md5 from 'js-md5'
const baseUrl = process.env.API_BASE_URL

function request (url, method, data) {
  // wx.showLoading({
  //   title: '加载中' // 数据请求前loading
  // })
  return new Promise((resolve, reject) => {
    const v = '1.0.0'
    const t = Date.now()
    const key = 'lockapi.jijiakeji-co.com'
    const s = md5(v + '&' + t + '&' + key)
    const constant = {
      v,
      t,
      s
    }
    const obj = Object.assign({}, constant, data)
    wx.request({
      url: baseUrl + url, // 接口地址
      method: method,
      data: obj,
      headers: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data.code === 0) {
          resolve(res.data)
        }
      },
      fail: function (res) {
        wx.showLoading({
          title: '请检查网络'
        })
        setTimeout(function () {
          wx.hideLoading()
        }, 5000)
      },
      complete: function () {
        // wx.hideLoading()
      }
    })
  })
}

function get (obj) {
  return request(obj.url, 'GET', obj.data)
}

function post (obj) {
  return request(obj.url, 'POST', obj.data)
}

export default {
  request,
  get,
  post
}
